package com.zego.wawaji.ui.wawa.synth.material;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.xlistview.ExListView;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

import java.util.List;

/**
 * 娃娃合成材料
 * Created by Su on 2018/3/12.
 */
public class SynthMaterialActivity extends BaseAppCompatActivity implements View.OnClickListener, PObserver {
    private CustomRecyclerView statusLayout;
    private ExListView exListView;
    private MaterialAdapter adapter;

    private UserDetail userDetail;
    private TextView goldNum;
    private List<Wawa> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.synth_material_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("购买材料");

        list = ModuleMgr.getWawaMgr().getMaterialList();
        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        if (list == null || list.size() <= 0) {
            statusLayout.showNoData();
        } else {
            userDetail = ModuleMgr.getCenterMgr().getMyInfo();
            View mViewTop = LayoutInflater.from(this).inflate(R.layout.synth_material_top, null);
            mViewTop.findViewById(R.id.btn_exchange).setOnClickListener(this);
            goldNum = (TextView) mViewTop.findViewById(R.id.gold_num);
            goldNum.setText(String.valueOf(userDetail.getWwCoinCz()));
            exListView = statusLayout.getExListView();
            exListView.setPullRefreshEnable(false);
            exListView.setPullLoadEnable(false);
            exListView.addHeaderView(mViewTop);
            adapter = new MaterialAdapter(this, list);
            exListView.setAdapter(adapter);
            statusLayout.showExListView();
        }
        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_exchange:
                PickerDialogUtil.showPayDialog(this);
                break;
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                userDetail = ModuleMgr.getCenterMgr().getMyInfo();
                if (goldNum != null)
                    goldNum.setText(String.valueOf(userDetail.getWwCoinCz()));
                break;
            case MsgType.MT_Close_Material:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}
